package com.base.cn.platform.os.controller.mobile.qa.question;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import com.base.cn.platform.os.service.course.qa.record.QaGainAttendRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LIZHIPENG
 * @create 2018-06-11 下午 3:21
 */
@Controller
@RequestMapping("/mobile")
public class MobileQuestionController extends MobileBaseController {

    @Autowired
    private QaQuestionService qaQuestionService;
    @Autowired
    private QaGainAttendRecordService qaGainAttendRecordService;

    /**
     * 加载问题列表
     * @param model Model
     * @param request HttpServletRequest
     * @param currentPage 页码
     * @param params 查询条件
     * @return String
     */
    @RequestMapping("/qa/queryQuestionList")
    public String queryQuestionList(Model model, HttpServletRequest request,
                                    @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                    @RequestBody Map<String, Object> params){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("selectFrom",2);
        Map<String, Object> dataMap = qaQuestionService.queryQuestionListPage(currentPage, 10, params, userId,false, false, true, false, false, false, false,false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/qa/question/ajax-question-list");
    }

    /**
     * 查看问题详情
     * @param model Model
     * @param request HttpServletRequest
     * @param questionId 问题ID
     * @return String
     */
    @RequestMapping("/qa/questionInfo")
    public String questionInfo(Model model, HttpServletRequest request,
                               @RequestParam("questionId") BigDecimal questionId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> question = qaQuestionService.findQuestionByIdForFront(questionId, userId, false, true, true, true, true, false, false);
        model.addAttribute("question", question);
        return resultPage("/qa/question/question-info");
    }

    /**
     * 去往我的问答页面
     * @param queryType 查询类型：1.我提问的-免费，2.我回答(评论)的，3.我提问的-付费，4.我围观的
     * @return
     */
    @RequestMapping("/uc/qa/toMyQuestionView")
    public String toMyQuestionView(@RequestParam(value = "queryType",defaultValue = "1")int queryType){
        if(queryType == 1){
            return resultPage("/qa/question/my/qa-myquestion-1");
        }else if(queryType == 2){
            return resultPage("/qa/question/my/qa-myquestion-2");
        }else if(queryType == 3){
            return resultPage("/qa/question/my/qa-myquestion-3");
        }else{
            return resultPage("/qa/question/my/qa-myquestion-4");
        }
    }

    /**
     * 去往我的问答收益
     * @return
     */
    @RequestMapping("/uc/qa/myQaProfit")
    public String myQaProfit(Model model, HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> map = new HashMap<>();
        map.put("receiptorId",userId);
        map.put("userType",3);
        //用户所有收益
        double allUserQaEarning = qaGainAttendRecordService.getUserQaEarning(map);
        model.addAttribute("allUserQaEarning",allUserQaEarning);
        map.put("status",1);
        //用户可提现收益
        double userAbleQaEarning = qaGainAttendRecordService.getUserQaEarning(map);
        model.addAttribute("userAbleQaEarning",userAbleQaEarning);
        return resultPage("/qa/question/my/qa-myProfit");
    }

    /**
     * 查询我的收益列表
     *
     * @param model
     * @param request
     * @param currentPage
     * @return
     */
    @RequestMapping("/uc/qa/ajax/myProfitList")
    public String myProfitList(Model model,
                               HttpServletRequest request,
                               @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> param = new HashMap<>();
        param.put("receiptorId",userId);
        param.put("userType",3);
        Map<String, Object> dataMap = qaGainAttendRecordService.queryGainAttendRecordListPage(currentPage, param, true, true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/qa/question/ajax-myProfit-list");
    }

    /**
     * 我的收益提现到账户
     *
     * @param request
     * @return
     */
    @RequestMapping("/uc/qa/extractEarning")
    @ResponseBody
    public Map<String,Object> extractEarning(HttpServletRequest request){
        Map<String ,Object> params = new HashMap<>();
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("receiptorId",userId);
        params.put("userType",3);
        return qaGainAttendRecordService.extractEarning(params);
    }

    /**
     * 查询我的问答列表
     * @param model Model
     * @param request HttpServletRequest
     * @param currentPage 页码
     * @param params 查询类型：
     * @return
     */
    @RequestMapping("/uc/qa/queryMyQuestionList")
    public String queryMyQuestionList(Model model, HttpServletRequest request,
                                      @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                      @RequestBody Map<String, Object> params){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        String _queryType = (String) params.get("queryType");
        int queryType = Integer.valueOf(_queryType);
        Map<String, Object> dataMap = qaQuestionService.queryMyQuestionList(currentPage, 10, userId, queryType, false, true, false, false, false, false, false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/qa/question/ajax-question-list");
    }

    /**
     * H5保存提问
     * @param request
     * @param params
     * @return
     */
    @RequestMapping("/uc/qa/saveQuestion")
    @ResponseBody
    public Map<String, Object> saveQuestion(HttpServletRequest request,
                                            @RequestBody Map<String, Object> params){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("userId", userId);
        return qaQuestionService.saveQuestion(params);
    }
}
